<template>
    <div class="product-list-container">
        <!-- 顶部导航栏 -->
        <header class="shop-header">
            <div class="header-content">
                <div class="shop-info">
                    <div class="shop-icon">
                        <img src="@/assets/img/other/崩坏星穹铁道.png" alt="崩坏星穹铁道" />
                    </div>
                </div>

                <nav class="category-nav">
                    <ul class="nav-list">
                        <li v-for="category in categories" :key="category.id" class="nav-item"
                            :class="{ active: selectedCategory === category.id }"
                            @click="handleCategorySelect(category.id)">
                            {{ category.name }}
                        </li>
                        <li class="nav-item more">
                            <el-icon>
                                <ArrowRight />
                            </el-icon>
                        </li>
                    </ul>
                </nav>

                <div class="header-actions">
                    <el-button type="primary" @click="goToCart" class="cart-button">
                        <el-icon>
                            <ShoppingCart />
                        </el-icon>
                        购物车
                    </el-button>
                </div>
            </div>
        </header>

        <!-- 排序栏 -->
        <div class="sort-bar">
            <div class="sort-content">
                <ul class="sort-list">
                    <li v-for="sort in sortOptions" :key="sort.id" class="sort-item"
                        :class="{ active: selectedSort === sort.id }" @click="handleSortSelect(sort.id)">
                        {{ sort.name }}
                    </li>
                </ul>
            </div>
        </div>

        <!-- 商品列表 -->
        <main class="product-main">
            <div class="product-grid">
                <div v-for="product in products" :key="product.id" class="product-card"
                    @click="handleProductClick(product.id)">
                    <div class="product-image" @click.stop="handleProductClick(product.id)">
                        <img :src="product.image" :alt="product.title" />
                        <div class="product-tag" :class="product.tagType">
                            {{ ProductTagLabel[product.tagType] || '' }}
                        </div>
                    </div>

                    <div class="product-info">
                        <h3 class="product-title">{{ product.title }}</h3>

                        <div class="product-price">
                            <span class="price-symbol">¥</span>
                            <span class="price-value">{{ product.price }}</span>
                        </div>
                    </div>
                </div>
            </div>
        </main>
    </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { Shop, ArrowRight, ShoppingCart } from '@element-plus/icons-vue'
import { getGoodsList } from '@/api/shop'
import { ElMessage } from 'element-plus'
import type { ProductTag } from '@/api/shop'
import { ProductTagLabel } from '@/api/shop'

const router = useRouter()

// 分类数据
const categories = ref([
    { id: 'all', name: '全部商品' },
    { id: 'coming', name: '即将上架' },
    { id: 'category1', name: '商品分类1' },
    { id: 'category2', name: '商品分类2' },
    { id: 'category3', name: '商品分类3' },
    { id: 'category4', name: '商品分类4' },
    { id: 'category5', name: '商品分类5' },
    { id: 'category6', name: '商品分类6' },
    { id: 'category7', name: '商品分类7' }
])

// 排序选项
const sortOptions = ref([
    { id: 'comprehensive', name: '综合' },
    { id: 'new', name: '上新' },
    { id: 'sales', name: '销量' },
    { id: 'price', name: '价格' }
])

// 商品数据（默认列表，接口失败时展示）
const products = ref([
    {
        id: 1,
        title: '「崩坏:星守铁道」秋季庆典系列Q版马口铁徽章',
        price: '15',
        image: 'https://via.placeholder.com/200x200/FF6B6B/FFFFFF?text=徽章1',
        // tag: '推荐',
        tagType: 'recommended'
    },
    {
        id: 2,
        title: '「崩坏:星守铁道」立绘系列马口铁徽章-存护命途',
        price: '18',
        image: 'https://via.placeholder.com/200x200/45B7D1/FFFFFF?text=徽章2',
        // tag: '推荐',
        tagType: 'recommended'
    },
    {
        id: 3,
        title: '「崩坏:星穹铁道」叽米的会客室系列马口铁徽章套组',
        price: '30',
        image: 'https://via.placeholder.com/200x200/96CEB4/FFFFFF?text=徽章3',
        // tag: '新品',
        tagType: 'new'
    },
    {
        id: 4,
        title: '「崩坏:星守铁道」星铁LAND系列马口铁徽章',
        price: '15',
        image: 'https://via.placeholder.com/200x200/FFEAA7/FFFFFF?text=徽章4',
        // tag: '新品',
        tagType: 'new'
    },
    {
        id: 5,
        title: '「崩坏:星守铁道」角色立绘系列徽章-姬子',
        price: '12',
        image: 'https://via.placeholder.com/200x200/FF9FF3/FFFFFF?text=姬子',
        // tag: '热销',
        tagType: 'hot'
    },
    {
        id: 6,
        title: '「崩坏:星守铁道」角色立绘系列徽章-布洛妮娅',
        price: '12',
        image: 'https://via.placeholder.com/200x200/54A0FF/FFFFFF?text=布洛妮娅',
        // tag: '热销',
        tagType: 'hot'
    },
    {
        id: 7,
        title: '「崩坏:星守铁道」限定版纪念徽章套装',
        price: '45',
        image: 'https://via.placeholder.com/200x200/5F27CD/FFFFFF?text=限定套装',
        // tag: '预定',
        tagType: 'limited'
    },
    {
        id: 8,
        title: '「崩坏:星守铁道」周边钥匙扣系列',
        price: '25',
        image: 'https://via.placeholder.com/200x200/00D2D3/FFFFFF?text=钥匙扣',
        // tag: '新品',
        tagType: 'new'
    }
])

// 加载商品列表
const loadProducts = async () => {
    try {
        const data: any = await getGoodsList()
        if (data && Array.isArray(data)) {
            products.value = data
        }
    } catch (e) {
        // 失败保留默认数据
        ElMessage.warning('获取商品列表失败，已展示默认数据')
    }
}

// 响应式数据
const selectedCategory = ref('all')
const selectedSort = ref('comprehensive')

// 方法
const handleCategorySelect = (categoryId: string) => {
    selectedCategory.value = categoryId
    // 这里可以添加分类筛选逻辑
}

const handleSortSelect = (sortId: string) => {
    selectedSort.value = sortId
    // 这里可以添加排序逻辑
}

const handleProductClick = (productId: number) => {
    router.push(`/shop/product/${productId}`)
}

const goToCart = () => {
    router.push('/shop/cart')
}

onMounted(() => {
    loadProducts()
})
</script>

<style scoped>
.product-list-container {
    background: #f8f9fa;
}

/* 顶部导航栏 */
.shop-header {
    background: #fff;
    border-bottom: 1px solid #e9ecef;
    position: sticky;
    top: 0;
    z-index: 100;
}

.header-content {
    margin: 0 auto;
    padding: 0 20px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 60px;
}

.shop-info {
    display: flex;
    align-items: center;
    margin-right: 40px;
}

.shop-icon {
    width: 80px;
    height: 80px;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
}

.shop-icon img {
    width: 100%;
    height: 100%;
    object-fit: contain;
}

.category-nav {
    flex: 1;
}

.nav-list {
    display: flex;
    list-style: none;
    margin: 0;
    padding: 0;
    gap: 32px;
}

.nav-item {
    padding: 8px 16px;
    border-radius: 20px;
    cursor: pointer;
    transition: all 0.3s ease;
    font-size: 14px;
    color: #666;
    white-space: nowrap;
}

.nav-item:hover {
    background: #f0f9ff;
    color: #409eff;
}

.nav-item.active {
    background: #409eff;
    color: #fff;
}

.nav-item.more {
    color: #999;
    padding: 8px 8px;
}

.header-actions {
    display: flex;
    align-items: center;
    gap: 12px;
}

.cart-button {
    display: flex;
    align-items: center;
    gap: 6px;
    height: 40px;
    padding: 0 16px;
    border-radius: 20px;
    font-size: 14px;
}

/* 排序栏 */
.sort-bar {
    background: #fff;
    border-bottom: 1px solid #e9ecef;
}

.sort-content {
    margin: 0 auto;
    padding: 0 20px;
}

.sort-list {
    display: flex;
    list-style: none;
    margin: 0;
    padding: 0;
    gap: 32px;
}

.sort-item {
    padding: 12px 0;
    cursor: pointer;
    transition: all 0.3s ease;
    font-size: 14px;
    color: #666;
    border-bottom: 2px solid transparent;
}

.sort-item:hover {
    color: #409eff;
}

.sort-item.active {
    color: #409eff;
    border-bottom-color: #409eff;
}

/* 商品列表 */
.product-main {
    margin: 0 auto;
    padding: 12px 12px;
}

.product-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
    gap: 12px;
}

.product-card {
    background: #fff;
    overflow: hidden;
    transition: box-shadow 0.25s ease, transform 0.25s ease;
    cursor: pointer;
    display: flex;
    flex-direction: column;
}

.product-card:hover {
    box-shadow: 0 12px 32px rgba(0, 0, 0, 0.12);
}

.product-image {
    width: 100%;
    aspect-ratio: 1 / 1;
    /* 正方形 */
    overflow: hidden;
    position: relative;
    background: #f6f6f6;
}

.product-image img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    /* 填充正方形区域，超出部分隐藏 */
    transition: none;
}

.product-info {
    width: 100%;
    padding: 12px 16px 14px;
    position: relative;
}

.brand-info {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 8px;
}

.brand-icon {
    width: 20px;
    height: 20px;
    border-radius: 4px;
    overflow: hidden;
}

.brand-icon img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.brand-name {
    font-size: 12px;
    color: #999;
}

.product-tag {
    position: absolute;
    top: 10px;
    right: 10px;
    padding: 4px 8px;
    border-radius: 999px;
    font-size: 12px;
    font-weight: 600;
    color: #fff;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.12);
}

.product-tag.recommended {
    background: #ff9500;
}

.product-tag.new {
    background: #52c41a;
}

.product-tag.hot {
    background: #ff4d4f;
}

.product-tag.limited {
    background: #722ed1;
}

.product-title {
    margin: 0 0 8px 0;
    font-size: 14px;
    font-weight: 600;
    color: #333;
    line-height: 1.4;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

.product-price {
    display: flex;
    align-items: baseline;
    gap: 2px;
}

.price-symbol {
    font-size: 14px;
    color: #ff4d4f;
    font-weight: 500;
}

.price-value {
    font-size: 18px;
    color: #ff4d4f;
    font-weight: 600;
}

/* 响应式设计 */
@media (max-width: 768px) {
    .header-content {
        padding: 0 16px;
        height: 50px;
    }

    .nav-list {
        gap: 16px;
    }

    .nav-item {
        padding: 6px 12px;
        font-size: 13px;
    }

    .sort-content {
        padding: 0 16px;
    }

    .sort-list {
        gap: 24px;
    }

    .product-main {
        padding: 16px;
    }

    .product-grid {
        grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
        gap: 16px;
    }

    .product-image {
        height: 240px;
    }
}

@media (max-width: 480px) {
    .product-grid {
        grid-template-columns: 1fr;
    }

    .nav-list {
        gap: 12px;
    }

    .nav-item {
        padding: 4px 8px;
        font-size: 12px;
    }
}
</style>
